package com.googlecode.economini.converter;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class IssueListConverter extends Converter<IssueListToken> {

  private IssueListConverter(TokenizerNode<IssueListToken> root) {
    super(root);
  }

  public static IssueListConverter newInstance() {
    AttributeMatcher dateAttributeMatcher = new AttributeMatcher.Builder()
        .withAttribute("class", "center cover-creation")
        .build();

    TokenizerNode<IssueListToken> dateNode = TokenizerNode.build(
        "div",
        dateAttributeMatcher,
       TokenFactories.valueTokenFacotry(IssueListToken.DATE),
       ImmutableList.<TokenizerNode<IssueListToken>>of());

    TokenizerNode<IssueListToken> imgSource = TokenizerNode.build(
        "img",
        AttributeMatcher.any(),
        TokenFactories.attributeTokenFacotry("src", IssueListToken.COVER_URL),
        ImmutableList.<TokenizerNode<IssueListToken>>of());

    AttributeMatcher coverAttributeMatcher = new AttributeMatcher.Builder()
        .matchesAll()
        .withAttribute("class", "print-cover-image")
        .build();

    TokenizerNode<IssueListToken> issueLink = TokenizerNode.build(
        "a",
        AttributeMatcher.any(),
        TokenFactories.attributeTokenFacotry("href", IssueListToken.URL),
        ImmutableList.of(imgSource));

    TokenizerNode<IssueListToken> coverDiv = TokenizerNode.build(
        "div",
        coverAttributeMatcher,
       null,
       ImmutableList.<TokenizerNode<IssueListToken>>of(issueLink));

    Splitter splitter = Splitter.on('/');
//    ImmutableList<String> nodePath = ImmutableList.of("", 
//        "html", "body", "div",
//        "div", "div", "div", "div", "div", "div");
    TokenizerNode<IssueListToken> rootNode = TokenizerNode.buildNodeForPath(
        Lists.newArrayList(
            splitter.split("root/html/body/div/div/div/div/div/div/div")),
        ImmutableList.of(dateNode, coverDiv));

     return new IssueListConverter(rootNode);
  }
}
